package sushil.luc.item;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.json.JSONObject;

import com.ugrokit.api.UgiTag;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import sushil.luc.dtos.ItemDTO;
import sushil.luc.dtos.ItemInfoDTO;
import sushil.luc.dtos.OrderDTO;
import sushil.luc.dtos.RfidInfoDTO;
import sushil.luc.dtos.TicketDTO;

import sushil.luc.msc.TruckerCheckManager;
import sushil.luc.network.Callback;
import sushil.luc.network.NetworkHandler;
import sushil.luc.ticket.Ticket;


public class ItemService {

    private final String webServer="http://sushilshilpakar.com.np/smartrfid/";

    /**
     * Fetches Rfid by item identifier from remote server
     * @param rfid
     * @return the corresponding item
     */
    // TODO fix concurrency here
    public Item fetchItemFromRfid(final UgiTag tag) {
        final List<ItemDTO> itemDtos = new ArrayList<ItemDTO>(1);
        try {
            final NetworkHandler networkHandler = NetworkHandler.getInstance();
           // String URL = "";
            String URL = webServer+"api/items/query.php?rfids="+tag.getEpc().toString();
            networkHandler.read(URL, ItemDTO.class, new Callback<ItemDTO>() {
                @Override
                public void callback(final ItemDTO myItemDto) {
                    itemDtos.add(myItemDto);
                    TruckerCheckManager.showDialog((new Item(myItemDto)),tag);
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
        Item item =null;

        if(itemDtos.size() > 0) {
            item = new Item(itemDtos.get(0));
        }

        return item;
    }
}
